﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using Rison.Web.Common;
using Rison.BusinessFacade.Entity.Biz;
using Rison.BusinessFacade.Facade.Biz;
using Rison.BusinessFacade.Common;
using Rison.BusinessFacade.Helper;

namespace Rison.Web.wscp
{
    public partial class CPByLogon : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                if (string.IsNullOrEmpty(Request.QueryString["id"]))
                {
                    Response.Redirect("../default.aspx");
                }

                labSBRQ.Text = DateTime.Now.ToString("yyyy年M月d日");

                if (!string.IsNullOrEmpty(Request.QueryString["id"]))
                {
                    try
                    {
                        CPResultDetailFacade facade = new CPResultDetailFacade();
                        DataSet depart = facade.GetDepartments("Mailbox.ID='" + Request.QueryString["id"] + "'");
                        if (depart != null && depart.Tables[0].Rows.Count == 1)
                        {
                            labCurDepart.Text = depart.Tables[0].Rows[0]["bmmc"].ToString();
                            depart.Dispose(); depart = null; 
                        }
                        facade = null;
                    }
                    catch
                    {
                        labCurDepart.Text = "评测内容";
                    }
                }
            }

            AppendStyle(rblResult_1); AppendStyle(rblResult_2);
            AppendStyle(rblResult_3); AppendStyle(rblResult_4);
            AppendStyle(rblResult_5); AppendStyle(rblResult_6);
        }

        private void AppendStyle(RadioButtonList rbl)
        {
            foreach (ListItem item in rbl.Items)
            {
                item.Attributes.CssStyle.Add("padding-right", "10px");
            }
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            Response.Redirect(Request.RawUrl);
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("CPDepartmentList.aspx");
        }


        // 当存在不满意的评价时，需要填写表格

        #region "   切换表格的显示隐藏  "
        //protected void rblResult_1_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    DisplaySuggestionTable();
        //}
        //protected void rblResult_2_SelectedIndexChanged1(object sender, EventArgs e)
        //{
        //    DisplaySuggestionTable();
        //}
        //protected void rblResult_3_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    DisplaySuggestionTable();
        //}
        //protected void rblResult_4_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    DisplaySuggestionTable();
        //}
        //protected void rblResult_5_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    DisplaySuggestionTable();
        //}
        //private void DisplaySuggestionTable()
        //{
        //    pnlSuggection.Visible = (rblResult_1.SelectedIndex > 2 & rblResult_1.SelectedIndex != 5) || (rblResult_2.SelectedIndex > 2 & rblResult_2.SelectedIndex != 5) || (rblResult_3.SelectedIndex > 2 & rblResult_3.SelectedIndex != 5) || (rblResult_4.SelectedIndex > 2 & rblResult_4.SelectedIndex != 5) || (rblResult_5.SelectedIndex > 2 & rblResult_5.SelectedIndex != 5);
        //}
        #endregion


        #region "  验证输入的数据  "
        private bool ValidateResult()
        {
            string msg = string.Empty;            

            if (rblResult_1.SelectedIndex < 0)
                msg = "请选择对‘一、依法行政’的评价！";
            else if (rblResult_2.SelectedIndex < 0)
                msg = "请选择对‘二、行政效能’的评价！";
            else if (rblResult_3.SelectedIndex < 0)
                msg = "请选择对‘三、政务公开’的评价！";
            else if (rblResult_4.SelectedIndex < 0)
                msg = "请选择对‘四、服务态度’的评价！";
            else if (rblResult_5.SelectedIndex < 0)
                msg = "请选择对‘五、清正廉洁’的评价！";

            if ((rblResult_1.SelectedIndex > 2 & rblResult_1.SelectedIndex != 5) || (rblResult_2.SelectedIndex > 2 & rblResult_2.SelectedIndex != 5) || (rblResult_3.SelectedIndex > 2 & rblResult_3.SelectedIndex != 5) || (rblResult_4.SelectedIndex > 2 & rblResult_4.SelectedIndex != 5) || (rblResult_5.SelectedIndex > 2 & rblResult_5.SelectedIndex != 5))
            {                
                if (string.IsNullOrEmpty(txtTime.Text))
                    msg = "请输入问题发生时间！";
                else if (string.IsNullOrEmpty(txtDW.Text.Trim()))
                    msg = "请输入涉及单位！";
                //else if (string.IsNullOrEmpty(txtRY.Text.Trim()))
                //    msg = "请输入问题涉及人员！";
                else if (string.IsNullOrEmpty(txtQK.Text.Trim()))
                    msg = "请输入问题基本情况！";
                else if (string.IsNullOrEmpty(txtXM.Text.Trim()))
                    msg = "请输入本人姓名！";
                else if (string.IsNullOrEmpty(txtLXDH.Text.Trim()))
                    msg = "请输入本人联系电话！";
                else if (rblJJ.SelectedItem == null || rblJJ.SelectedIndex < 0)
                    msg = "请选择是否已解决！";
                else if (!StringHelper.ValidLength(txtDW.Text.Trim(), 100))
                    msg = "问题涉及单位长度不能超过50个汉字！";
                else if (!StringHelper.ValidLength(txtRY.Text.Trim(), 100))
                    msg = "问题涉及人员长度不能超过50个汉字！";
                else if (!StringHelper.ValidLength(txtQK.Text.Trim(), 2000))
                    msg = "问题基本情况长度不能超过1000个汉字！";
                else if (!StringHelper.ValidLength(txtXM.Text.Trim(), 100))
                    msg = "本人姓名长度不能超过50个汉字！";
                else if (!StringHelper.ValidLength(txtLXDH.Text.Trim(), 100))
                    msg = "本人联系电话长度不能超过100个字符！";
                else if (!StringHelper.ValidLength(txtJJ.Text.Trim(), 1000))
                    msg = "建议内容长度不能超过500个汉字！";
            }

            if (!string.IsNullOrEmpty(msg))
                this.Page.ClientScript.RegisterStartupScript(this.GetType(), "msg", "<script>alert('" + msg + "');</script>");

            return string.IsNullOrEmpty(msg);
        }
        #endregion


        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (!ValidateResult()) return;

            WSCPResult result = new WSCPResult();
            result.BMGuid = Request.QueryString["id"];
            result.BMMC = labCurDepart.Text;
            result.YFXZ = rblResult_1.SelectedItem.Text;
            result.XZXN = rblResult_2.SelectedItem.Text;
            result.ZWGK  = rblResult_3.SelectedItem.Text;
            result.FWTD  = rblResult_4.SelectedItem.Text;
            result.QZLJ = rblResult_5.SelectedItem.Text;
            result.GZCX = rblResult_6.SelectedItem.Text;

            if (txtTime.Text.Trim()!="")
            {
            result.OccurrenceTime = Convert.ToDateTime(txtTime.Text.Trim());
            }
            result.UnitsInvolved = txtDW.Text.Trim();
            result.PeopleInvolved = txtRY.Text.Trim();
            result.WTQK = txtQK.Text.Trim();
            result.Suggestion = txtJJ.Text.Trim();

            result.XM = txtXM.Text.Trim();
            result.LXDH = txtLXDH.Text.Trim();

            result.FromType = cpType.来信.ToString();

            List<WSCPResult> results = new List<WSCPResult>();
            results.Add(result);

            CPResultDetailFacade facade = new CPResultDetailFacade();
            bool success = facade.Save(results, Request.UserHostAddress, false);

            result = null; facade = null;

            if (success)
            {
                this.Page.ClientScript.RegisterStartupScript(this.GetType(), "msg", "<script>alert('您已完成了对所有部门的评价，感谢您对我们工作的支持！');location.href='../default.aspx';</script>");
            }
            else
                this.Page.ClientScript.RegisterStartupScript(this.GetType(), "msg", "<script>alert('对不起，提交失败！可能是因为网络比较繁忙，请稍后再试...');</script>");

        }

        #region "  保存——原始  "
        private void SaveData()
        {
            List<CPResult> results = new List<CPResult>();
            CPResult result = new CPResult(); results.Add(result);
            result.BMGuid = Request.QueryString["id"]; result.CPXM = "依法行政"; result.CPJG = rblResult_1.SelectedItem.Text;
            result = new CPResult(); results.Add(result);
            result.BMGuid = Request.QueryString["id"]; result.CPXM = "行政效能"; result.CPJG = rblResult_2.SelectedItem.Text;
            result = new CPResult(); results.Add(result);
            result.BMGuid = Request.QueryString["id"]; result.CPXM = "政务公开"; result.CPJG = rblResult_3.SelectedItem.Text;
            result = new CPResult(); results.Add(result);
            result.BMGuid = Request.QueryString["id"]; result.CPXM = "服务态度"; result.CPJG = rblResult_4.SelectedItem.Text;
            result = new CPResult(); results.Add(result);
            result.BMGuid = Request.QueryString["id"]; result.CPXM = "清正廉洁"; result.CPJG = rblResult_5.SelectedItem.Text;

            CPResultDetail detail = null;
            if (pnlSuggection.Visible)
            {
                detail = new CPResultDetail();
                detail.WTSJ = Convert.ToDateTime(txtTime.Text.Trim());
                detail.SJDW = txtDW.Text.Trim();
                detail.SJRY = txtRY.Text.Trim();
                detail.WTQK = txtQK.Text.Trim();
                detail.JYNR = txtJJ.Text.Trim();
                detail.XM = txtXM.Text.Trim();
                detail.LXDH = txtLXDH.Text.Trim();
            }

            CPResultDetailFacade facade = new CPResultDetailFacade();
            bool success = facade.Save_original (results, detail, Request.UserHostAddress);

            results = null; detail = null; facade = null;

            if (success)
            {
                this.Page.ClientScript.RegisterStartupScript(this.GetType(), "msg", "<script>alert('您已完成了对所有部门的评价，感谢您对我们工作的支持！');location.href='../default.aspx';</script>");
            }
            else
                this.Page.ClientScript.RegisterStartupScript(this.GetType(), "msg", "<script>alert('对不起，提交失败！可能是因为网络比较繁忙，请稍后再试...');</script>");

        }
        #endregion


    }
}
